$(function() {
	reloadTree('1');
	$("body").bind("mousedown", function(event){
		if (!(event.target.id == "menuBtn" || event.target.id == "DropdownMenuBackground" || $(event.target).parents("#DropdownMenuBackground").length>0)) {
			hideMenu();
		}
	});
	
	//jQuery 表单验证֤
	$('#userForm').validate();

	//用户名输入框失去焦点时调用
	$('#user_code').blur(function(){      
		var user_code=$("#user_code").val();
		var edit_flag=$("#edit_flag").val();
		checkUserCode(user_code, edit_flag);
	});
	
	//提交表单
	$('#saveUserAuthButt').click(function() { 
		$('#userForm').submit();
	});
});

//校验用户名
function checkUserCode(user_code, edit_flag) {
	$.ajax({   
		url: 'checkUser.action',   
		type:'POST',   
		dataType: 'json',   
		data:"user.user_code="+user_code+"&edit="+edit_flag,   
		beforeSend:function(xmlhttprequest){
		},
		success:function(data){
			var isExists = data.isExists;
			if(isExists == true || isExists=='true') {
				$('#user_code_msg').html("此用户名已被使用，请重新填写用户名！");
				$('#user_code').focus();
			}
			else {
				$('#user_code_msg').html("");
			}
		},
		error:function(){
			//alert("exect checkUserName()出错了！");
		}
	});
}

/***************************************** 以下是下拉框树的实现 *****************************************/
var zTree1;
var setting;
setting = {
		async: true,
		asyncUrl: getUrl,
		asyncDataFilter:dataFilter,
		isSimpleData: true,
		treeNodeKey: "department_code",
		treeNodeParentKey: "parent_code",
		nameCol : "department_name",
		fontCss: {color: "blue"},
		callback: {
			click: zTreeOnClick
		}
	};

function dataFilter(id, node, data) {
	for(var i=0; i<data.length; i++) {
		data[i].isParent=true;
	}
	return data;
}

function showMenu() {
	var obj = $("#department_name");
	var offset = $("#department_name").offset();
	$("#DropdownMenuBackground").css({left:offset.left + "px", top:offset.top + obj.outerHeight() + "px"}).slideDown("fast");
}
function hideMenu() {
	$("#DropdownMenuBackground").fadeOut("fast");
}

function zTreeOnClick(event, treeId, treeNode) {
	if (treeNode) {
		$("#department_code").attr("value", treeNode.department_code);
		$("#department_name").attr("value", treeNode.department_name);
		hideMenu();
	}
}

function getUrl(node) {
	return '../department/listDepartmentAsJsonArray.action?department.valid_flag=1&department.parent_code=' + node.department_code;
}

function reloadTree(parent_code) {
	$.ajax({
		url: '../department/listDepartment.action',   
		type:'POST',   
		dataType: 'json',   
		data:"department.valid_flag=1&department.parent_code="+parent_code,   
		beforeSend:function(xmlhttprequest){
		},
		success:function(data){
			hideMenu();
			var results = data.results;
			zTree1 = $("#dropdownMenu").zTree(setting, clone(results));
		},
		error:function(action, errortype, desc){
			alert("加载部门信息失败，请检查网络连接或稍后再试！");
		}
	});
}

/**构造树处理json数据使用的方法*/
function clone(jsonObj, newName) {
	var buf;
	if (jsonObj instanceof Array) {
		buf = [];
		var i = jsonObj.length;
		while (i--) {
			jsonObj[i].isParent=true;
			buf[i] = clone(jsonObj[i], newName);
		}
		return buf;
	} else if (typeof jsonObj == "function") {
		return jsonObj;
	} else if (jsonObj instanceof Object) {
		buf = {};
		for ( var k in jsonObj) {
			if (k != "parentNode") {
				buf[k] = clone(jsonObj[k], newName);
				if (newName && k == "name")
					buf[k] += newName;
			}
		}
		return buf;
	} else {
		return jsonObj;
	}
}
